<template>
  <div class="login-class">
    <div class="login">
      <!-- 登录表单 -->
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <!-- 用户名 -->
        <el-form-item label="账号" prop="u_name">
          <el-input v-model="ruleForm.u_name" placeholder="请输入账号"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item>
          <el-button style="margin-left: 100px" type="primary" round @click="login">立即登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'login',
  props: [''],
  data () {
    return {
      ruleForm: {
        u_name: 'admin',
        password: '123456'
      },
      rules: {
        u_name: [
          { required: true, message: '请输入账号名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入账号密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ]
      }
    }
  },

  components: {},

  computed: {},

  beforeMount () { },

  mounted () { },

  methods: {
    login () {
      this.$refs.ruleForm.validate(async (valid) => {
        // console.log(valid)
        if (!valid) {
          return this.$message.warning('请输入用户名和密码')
        }
        const { data } = await this.$http.post('/users', this.ruleForm)
        // console.log(data)
        if (data.u_name !== 'admin') {
          return this.$message.error('登陆失败')
        }
        this.$message.success('登陆成功')
        // 储存token值
        // window.sessionStorage.setItem('token', data.data.token)
        // 跳转页面
        this.$router.push('/home')
      })
    }
  },

  watch: {}
}
</script>
<style lang="less" scoped>
.login-class {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: url(../../assets/bj.png);
  background-size: cover;
  .login {
    position: absolute;
    top: 50%;
    left: 50%;
    box-sizing: border-box;
    padding: 50px 15px;
    transform: translate(-50%, -50%);
    width: 500px;
    height: 300px;
    background-color: rgba(222, 228, 116, 0.5);
    .el-form {
      margin: 0 56px 0 -11px;
      /deep/ .el-form-item__label {
        color: #000;
      }
      /deep/ .el-form-item__content {
        line-height: 60px;
      }
    }
  }
}
</style>
